package com.zbs.framework.dao.passport.base.service.impl;

import com.zbs.framework.dao.passport.base.repository.MNLoginRecordRepository;
import com.zbs.framework.dao.passport.base.service.LoginRecordService;
import com.zbs.framework.dao.passport.entity.MNLoginRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.UUID;

@Service
@Transactional
public class LoginRecordServiceImpl implements LoginRecordService {

    @Autowired
    private MNLoginRecordRepository loginRecordRepository;

    @Override
    public MNLoginRecord refresh(String uuid, String entryType) {
        MNLoginRecord loginRecord = loginRecordRepository.findByUuidAndEntryType(uuid, entryType);
        if (loginRecord == null){
            loginRecord = new MNLoginRecord();
            loginRecord.setUuid(uuid);
            loginRecord.setEntryType(entryType);
        }
        //生成新的token
        String newToken = UUID.randomUUID().toString();
        loginRecord.setToken(newToken);
        //更新最近登录时间
        loginRecord.setLastLoginTime(new Date());
        this.loginRecordRepository.save(loginRecord);

        return loginRecord;
    }
}
